<?php

namespace farm\traits;

trait BaseUtils
{
    public static function now(): string
    {
        return date("Y-m-d H:i:s");
    }

    public static function pic($id): string
    {
        return asset("storage/$id");
    }

    /**
     * 驼峰命名转下划线命名
     * 思路:
     * 小写和大写紧挨一起的地方,加上分隔符,然后全部转小写
     * @param string $camelCaps
     * @param string $separator
     * @return string
     */
    public static function uncamelize(string $camelCaps, string $separator = '_'): string
    {
        $str = preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps);

        $str = preg_replace('/([0-9])([A-Z])/', "$1" . $separator . "$2", $str);

        return strtolower($str);
    }

    /**
     * 下划线转驼峰
     * 思路:
     * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
     * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
     * @param string $uncamelize
     * @param bool $class
     * @param string $separator
     * @return string
     */
    function camelize(string $uncamelize, bool $class = true, string $separator = '_'): string
    {
        $uncamelize = $separator . str_replace($separator, " ", strtolower($uncamelize));

        $uncamelize = ltrim(str_replace(" ", "", ucwords($uncamelize)), $separator);

        return $class ? ucwords($uncamelize) : $uncamelize;
    }
}
